package cn.com.iscs.sqlProject.controller;

import cn.com.iscs.sqlProject.util.UKID;
import cn.com.iscs.sqlProject.entity.*;
import cn.com.iscs.sqlProject.mapper.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.*;

import static cn.com.iscs.sqlProject.constants.Constants.THREAD_LOCAL;

/**
 * sqlProject生成权限sql文件
 *
 * @author yejunjun on 2016/11/24.
 * @ApiFile:true
 * @ApiGroup:sqlProject生成权限sql文件
 */
@Controller
@RequestMapping(value = "/authoritySqlBuild")
public class AuthoritySqlBuildAction {
	private Logger logger = LoggerFactory.getLogger(AuthoritySqlBuildAction.class);


	@Resource
	private CfOperationUnitMapper cfOperationUnitMapper;

	@Resource
	private AuFunctionMapper auFunctionMapper;
	@Resource
	private AuOperationFunctionMapper auOperationFunctionMapper;

	@Resource
	private CfOperationRouteMapper cfOperationRouteMapper;
	@Resource
	private BaIndustryExhibitionMapper baIndustryExhibitionMapper;
	@Resource
	private BaIndustryMapper baIndustryMapper;
	@Resource
	private CmBusinessServiceRuleMapper cmBusinessServiceRuleMapper;
	@Resource
	private BaDefinedCodeMapper baDefinedCodeMapper;

	@Resource
	private AuBusinessRelationMapper auBusinessRelationMapper;
	@Resource
	private CfCardLabelMapper cfCardLabelMapper;


	/*
	 * @ApiMethod:true
     * @ApiMethodName:保存卡片信息
     * @ApiRequestParamsDes:
     * |cardLabelName|json字符串|必须|||卡片名称|
     * |cardLabelType|json字符串|必须|||卡片类型|
     * @ApiRequestParams:
		{
			"cardLabelName":"商品展示",
			"cardLabelType":"CommodityShow"
		}
      @ApiResponseDes:
     * @ApiResponse:
     * 成功返回:
      {"code":"0","sql":["xxxx","xxxx"]}
     * @ApiMethodEnd
     */
	@Transactional(rollbackFor = Exception.class)
	@RequestMapping(value = "/saveCardForm")
	public
	@ResponseBody
	JSONObject saveCardForm(@RequestBody JSONObject request) throws Exception {
/*-----------------------------------------------模拟请求-------------------------------------------------------*/
//		request = new JSONObject();
//		request.put("cardLabelName","商品展示");
//		request.put("cardLabelType","CommodityShow");
/*-----------------------------------------------/模拟请求-------------------------------------------------------*/

		logger.info(request.toString());
		JSONObject response = new JSONObject();
		CfCardLabel cfCardLabel = JSON.parseObject(request.toString(), CfCardLabel.class);

		HashMap<String, Object> cardTypeQuery = new HashMap<>();
		cardTypeQuery.put("card_label_type", cfCardLabel.getCardLabelType());
		List<CfCardLabel> cfCardLabels = cfCardLabelMapper.getsByMap(cardTypeQuery);
		if (!cfCardLabels.isEmpty()) {
			response.put("sql", "该卡片类型已存在");
			return response;
		}

		cfCardLabel.setCardLabelUkid(getTrueCardUkid());
		cfCardLabelMapper.insert(cfCardLabel);
		//获取sql
		List<String> sqls = THREAD_LOCAL.get();
		THREAD_LOCAL.set(new ArrayList<String>());

		//删除插入的sql
		cfCardLabelMapper.delete(cfCardLabel.getCardLabelUkid());
		response.put("sql", sqls);
		return response;
	}

	/*
	 * @ApiMethod:true
     * @ApiMethodName:获取表单信息
     * @ApiRequestParamsDes:
     * @ApiRequestParams:
      @ApiResponseDes:
     * |operationUnitList|json数组|必须|||任务单元和职能树关系|
     * |id|Long|必须|||任务单元id|
     * |desc|String|必须|||任务单元描述|
     * |functionTree|JSON数组|必须|||职能树|
     * |id|Long|必须|||职能id|
     * |name|Long|必须|||职能名字|
     * |childFunction|Long|必须|||子级职能|
     * |defined|JSON数组|必须|||行业信息|
     * |code|String|必须|||业务框架第一级 行业code|
     * |name|String|必须|||业务框架第一级 行业名称|
     * |type|String|必须|||行业类型|
     * @ApiResponse:
     * 成功返回:
      				{
		  "operationUnitList": [
		    {
		      "id": 100304,
		      "desc": "100304|核实退货信息|CheckPurchaseReturn"
		    },
		    {
		      "id": 100311,
		      "desc": "100311|缺货订单处理|abnormalOrderHandle"
		    }
		  ],
		  "defined": [
		    {
		      "code": "F",
		      "name": "批发和零售",
		      "type": "INDUSTRY_TYPE"
		    },
		    {
		      "code": "G01",
		      "name": "交通运输",
		      "type": "INDUSTRY_TYPE"
		    }
		  ],
		  "functionTree": [
		    {
		      "id": 1,
		      "name": "产",
		      "childFunction": [
		        {
		          "id": 15,
		          "name": "安全",
		          "childFunction": [

		          ]
		        },
		        {
		          "id": 11,
		          "name": "生产",
		          "childFunction": [

		          ]
		        }
		      ]
		    },
		    {
		      "id": 2,
		      "name": "销",
		      "childFunction": [
		        {
		          "id": 21,
		          "name": "销售",
		          "childFunction": [

		          ]
		        },
		        {
		          "id": 22,
		          "name": "渠道",
		          "childFunction": [

		          ]
		        }
		      ]
		    }
		  ]
		}
		],
		"code": 0
		}
     * @ApiMethodEnd
     */
	@RequestMapping(value = "/getForm")
	public
	@ResponseBody
	JSONObject getForm() {
		JSONObject response = new JSONObject();
		response.put("code", 0);
		HashMap queryMap = new HashMap();
		//职能树
		List<AuFunction> dataSource = auFunctionMapper.getsByMap(queryMap);
		JSONArray functionTree = getChildFunction(dataSource, 0L);
		//路由任务单元选择
		List<CfOperationUnit> operationUnits = cfOperationUnitMapper.getsByMap(queryMap);
		JSONArray operationUnitTree = getOperationUnitTree(operationUnits);

		List<BaIndustryExhibition> industryCodes = baIndustryExhibitionMapper.getAll();
		List<BaIndustry> serviceCodes = getByIndustries(industryCodes);

		response.put("defined", convertServiceCodes(serviceCodes));
		response.put("functionTree", functionTree);
		response.put("operationUnitList", operationUnitTree);
		return response;
	}


	/*
	 * @ApiMethod:true
     * @ApiMethodName:保存表单信息
     * @ApiRequestParamsDes:
     * |auOperationFunction|json字符串|必须|||任务单元和职能树关系|
     * |cfOperationUnit|json字符串|必须|||任务单元|
     * |cfOperationRouteList|json数组|不必须|||任务路由数组|
     * |auFunctionUkid|String|必须|999||职能树id|
     * |premiseDesc|String|必须|测试插入||任务单元描述|
     * |needAuth|String|必须|1||是否判断权限，0 不判断。1判断|
     * |needJobPoint|String|必须|1||是否判断作业点，0 不判断。1判断|
     * |clientShow|String|必须|1||展示客户端 1:手机端, -1:PC端, 0:PC和手机端都显示, 2:PC和手机端均不显示|
     * |isLongTask|String|必须|1||是否是长期任务 1 是，0 不是|
     * |taskType|String|必须|testInsert||任务单元英文名称|
     * |needReallot|String|必须|1||是否重新分配参与者 1 是，0 不是|
     * |processName|String|必须|测试插入||任务单元中文名称|
     * |groupId|String|不必须|11||路由组id|
     * |opSeq|String|必须|1||卡片是否推送。1 推送。0 不推送|
     * |condition|String|不必须|||符合条件|
     * |nextOperationUnitUkid|String|nextOperationUnitUkid和crtOperationUnitUkid必须选填一个|111111||下一个任务单元id|
     * |crtOperationUnitUkid|String|nextOperationUnitUkid和crtOperationUnitUkid必须选填一个|1233322||上一个任务单元id|
     * |auBusinessRelationList|String|不必须|||任务单元与业务框架关系数组|
     * |businessScopeCode|String||F||行业CODE(业务范围类型)|
     * |businessType|String||BASE||业务类型|
     * |businessCode|String||BASE||业务编码|
     * @ApiRequestParams:
		{
		  "auOperationFunction": {
		    "auFunctionUkid": "999"
		  },
		  "cfOperationRouteList": [
		    {
		      "crtOperationUnitUkid": "1300352",
		      "groupId": "123",
		      "opSeq": "1",
		      "condition": ""
		    },
		    {
		      "groupId": "123",
		      "opSeq": "1",
		      "condition": "",
		      "nextOperationUnitUkid": "1300352"
		    }
		  ],
		  "cfOperationUnit": {
		    "premiseDesc": "测试插入",
		    "needAuth": "1",
		    "clientShow": "1",
		    "needJobPoint": "1",
		    "isLongTask": "1",
		    "taskType": "testInsert",
		    "needReallot": "1",
		    "processName": "测试插入"
		  },
		  "auBusinessRelationList": [
		    {
		      "businessCode": "BASE",
		      "businessType": "BASE",
		      "businessScopeCode": "BASE"
		    },
		    {
		      "businessCode": "BASE",
		      "businessType": "BASE",
		      "businessScopeCode": "F"
		    }
		  ]
		}
      @ApiResponseDes:
     * @ApiResponse:
     * 成功返回:
      {"code":"251003","sql":["xxxx","xxxx"]}
     * @ApiMethodEnd
     */
	@Transactional(rollbackFor = Exception.class)
	@RequestMapping(value = "/saveForm")
	public
	@ResponseBody
	JSONObject saveForm(@RequestBody JSONObject request) throws Exception {

/*-----------------------------------------------模拟请求-------------------------------------------------------*/
//		request = new JSONObject();
//		JSONObject cfOperationUnitrequest = new JSONObject();
//		JSONObject auOperationFunctionrequest = new JSONObject();
//		JSONArray cfOperationRouteListRequest = new JSONArray();
//		request.put("cfOperationUnit",cfOperationUnitrequest);
//		request.put("auOperationFunction",auOperationFunctionrequest);
//		cfOperationUnitrequest.put("premiseDesc","测试插入");
//		cfOperationUnitrequest.put("processName","测试插入");
//		cfOperationUnitrequest.put("taskType","testInsert");
//		cfOperationUnitrequest.put("isLongTask","1");
//		cfOperationUnitrequest.put("clientShow","1");
//		cfOperationUnitrequest.put("needJobPoint","1");
//		cfOperationUnitrequest.put("needAuth","1");
//		cfOperationUnitrequest.put("needReallot","1");
//
//		auOperationFunctionrequest.put("auFunctionUkid","999");
//
//		//前置任务
//		JSONObject crtOperationUnit = new JSONObject();
//		crtOperationUnit.put("crtOperationUnitUkid","1300352");
//		crtOperationUnit.put("groupId","123");
//		crtOperationUnit.put("opSeq","1");
//		crtOperationUnit.put("condition","");
//		cfOperationRouteListRequest.add(crtOperationUnit);
//		//后置任务
//		JSONObject nextOperationUnit = new JSONObject();
//		nextOperationUnit.put("nextOperationUnitUkid","1300352");
//		nextOperationUnit.put("groupId","123");
//		nextOperationUnit.put("opSeq","1");
//		nextOperationUnit.put("condition","");
//		cfOperationRouteListRequest.add(nextOperationUnit);
//
//		request.put("cfOperationRouteList",cfOperationRouteListRequest);
//
//		JSONObject auBusinessRelation1 = new JSONObject();
//		auBusinessRelation1.put("businessScopeCode","BASE");
//		auBusinessRelation1.put("businessType","BASE");
//		auBusinessRelation1.put("businessCode","BASE");
//
//		JSONObject auBusinessRelation2 = new JSONObject();
//		auBusinessRelation2.put("businessScopeCode","F");
//		auBusinessRelation2.put("businessType","ASSETS_TYPE_X");
//		auBusinessRelation2.put("businessCode","BASE");
//		JSONArray auBusinessRelationListRequest = new JSONArray();
//		auBusinessRelationListRequest.add(auBusinessRelation1);
//		auBusinessRelationListRequest.add(auBusinessRelation2);
//		request.put("auBusinessRelationList",auBusinessRelationListRequest);
//
//
//
//		logger.info(request.toString());
/*-----------------------------------------------/模拟请求-------------------------------------------------------*/

		JSONObject response = new JSONObject();
		response.put("code", "0");
		String cfOperationUnitRequest = request.getString("cfOperationUnit");
		String cfOperationRouteList = request.getString("cfOperationRouteList");
		String auOperationFunctionRequest = request.getString("auOperationFunction");
		String auBusinessRelationRequest = request.getString("auBusinessRelationList");
		if (checkParams(cfOperationUnitRequest, auOperationFunctionRequest)) {
			response.put("code", "100");
			return response;
		}


		CfOperationUnit cfOperationUnit = JSON.parseObject(cfOperationUnitRequest, CfOperationUnit.class);
		//对OperationUnit进行同步处理
		response = insertOperationUnit(cfOperationUnit,response);
		if(!"0".equals(response.getString("code"))){
			return response;
		}


		AuOperationFunction auOperationFunction = JSON.parseObject(auOperationFunctionRequest, AuOperationFunction.class);
		auOperationFunction.setAuOperationFunctionUkid(UKID.getUKID());
		auOperationFunction.setAuOperationUnitUkid(cfOperationUnit.getOperationUnitUkid());
		auOperationFunctionMapper.insert(auOperationFunction);


		List<CfOperationRoute> cfOperationRoutes = new ArrayList<>();
		if (cfOperationRouteList != null && !cfOperationRouteList.isEmpty()) {
			cfOperationRoutes = JSON.parseArray(cfOperationRouteList, CfOperationRoute.class);
			for (CfOperationRoute cfOperationRoute : cfOperationRoutes) {
				cfOperationRoute.setRouteUkid(UKID.getUKID());
				if (cfOperationRoute.getCrtOperationUnitUkid() == null) {
					cfOperationRoute.setCrtOperationUnitUkid(cfOperationUnit.getOperationUnitUkid());
				} else {
					cfOperationRoute.setNextOperationUnitUkid(cfOperationUnit.getOperationUnitUkid());
				}
				cfOperationRouteMapper.insert(cfOperationRoute);
			}
		}

		//任务单元(权限)与业务框架关系
		List<AuBusinessRelation> auBusinessRelations = new ArrayList<>();
		List<AuBusinessRelation> auBusinessRelationsDel = new ArrayList<>();
		if (auBusinessRelationRequest != null && !auBusinessRelationRequest.isEmpty()) {
			auBusinessRelations = JSON.parseArray(auBusinessRelationRequest, AuBusinessRelation.class);
			for (AuBusinessRelation auBusinessRelation : auBusinessRelations) {
				if (!"BASE".equals(auBusinessRelation.getBusinessType()) && "BASE".equals(auBusinessRelation.getBusinessCode())) {
					List<CmCustomerServiceCode> selectCodes = getSelectCodes(auBusinessRelation.getBusinessScopeCode(), auBusinessRelation.getBusinessType());
					List<VCustomerServiceCode> vCustomerServiceCodes = convertCustomerServiceCode(selectCodes);
					for (VCustomerServiceCode vCustomerServiceCode : vCustomerServiceCodes) {
						AuBusinessRelation auBusinessRelationNew = new AuBusinessRelation();
						auBusinessRelationNew.setBusinessScopeCode(auBusinessRelation.getBusinessScopeCode());
						auBusinessRelationNew.setBusinessType(auBusinessRelation.getBusinessType());
						auBusinessRelationNew.setBusinessCode(vCustomerServiceCode.getCode());
						auBusinessRelationNew.setOperationUkid(cfOperationUnit.getOperationUnitUkid());
						auBusinessRelationNew.setRoleBusinessUkid(UKID.getUKID());
						auBusinessRelationMapper.insert(auBusinessRelationNew);
						auBusinessRelationsDel.add(auBusinessRelationNew);
					}
				} else {
					auBusinessRelation.setOperationUkid(cfOperationUnit.getOperationUnitUkid());
					auBusinessRelation.setRoleBusinessUkid(UKID.getUKID());
					auBusinessRelationMapper.insert(auBusinessRelation);
					auBusinessRelationsDel.add(auBusinessRelation);
				}
			}
		}

		//获取sql
		List<String> sqls = THREAD_LOCAL.get();
		THREAD_LOCAL.set(new ArrayList<String>());

		//删除插入的sql
		cfOperationUnitMapper.delete(cfOperationUnit.getOperationUnitUkid());
		auOperationFunctionMapper.delete(auOperationFunction.getAuOperationFunctionUkid());
		if (!cfOperationRoutes.isEmpty()) {
			for (CfOperationRoute cfOperationRoute : cfOperationRoutes) {
				cfOperationRouteMapper.delete(cfOperationRoute.getRouteUkid());
			}
		}

		//删除任务单元(权限)与业务框架关系
		if (!auBusinessRelations.isEmpty()) {
			for (AuBusinessRelation auBusinessRelation : auBusinessRelationsDel) {
				auBusinessRelationMapper.delete(auBusinessRelation.getRoleBusinessUkid());
			}
		}

		response.put("sql", sqls);
		return response;
	}

	private  synchronized JSONObject insertOperationUnit(CfOperationUnit cfOperationUnit,JSONObject response) {
		Long operaitonUnitUkid = getTrueUkid(); //生成1开头6位UKID
		cfOperationUnit.setOperationUnitUkid(operaitonUnitUkid);
		HashMap<String, Object> operationTypeQuery = new HashMap<>();
		operationTypeQuery.put("task_type", cfOperationUnit.getTaskType());
		List<CfOperationUnit> cfOperationUnits = cfOperationUnitMapper.getsByMap(operationTypeQuery);
		if (!cfOperationUnits.isEmpty()) {
			response.put("code","500");
			response.put("sql", "该任务类型已存在");
			return response;
		}
		cfOperationUnitMapper.insert(cfOperationUnit);
		return response;
	}

	private boolean checkParams(String cfOperationUnitRequest, String auOperationFunctionRequest) {
		return cfOperationUnitRequest == null || cfOperationUnitRequest.isEmpty() || auOperationFunctionRequest == null || auOperationFunctionRequest.isEmpty();
	}


	private JSONArray getOperationUnitTree(List<CfOperationUnit> operationUnits) {
		JSONArray operationUnitTree = new JSONArray();
		for (CfOperationUnit cfOperationUnit : operationUnits) {
			JSONObject json = new JSONObject();
			json.put("id", cfOperationUnit.getOperationUnitUkid());
			json.put("desc", cfOperationUnit.getOperationUnitUkid() + "|" + cfOperationUnit.getProcessName() + "|" + cfOperationUnit.getTaskType());
			operationUnitTree.add(json);
		}
		return operationUnitTree;
	}


	/*
	 * @ApiMethod:true
     * @ApiMethodName:根据业务框架第一级及第二级信息，获取第三级内容
     * @ApiRequestParamsDes:
     * |type|String|必须|999||业务框架第二级|
     * |code|String|必须|999||业务框架第一级|
	 * @ApiRequestParams:
	 * {
	 "type":"ASSETS_TYPE_X",
	 "code":"F"
	 }
	  @ApiResponseDes:
	 |code|String|必须|online_retail||业务框架第三级CODE|
	 |name|String|必须|网上零售店||业务框架第三级中文|
	 |type|String|必须|ASSETS_TYPE_X||对应业务框架第二级|
     * @ApiResponse:
     * 成功返回:
	 [
	 {
	 "code": "online_retail",
	 "name": "网上零售店",
	 "type": "ASSETS_TYPE_X"
	 },
	 {
	 "code": "owner_transport",
	 "name": "自建运输",
	 "type": "ASSETS_TYPE_X"
	 },
	 {
	 "code": "vehicle",
	 "name": "车",
	 "type": "ASSETS_TYPE_X"
	 },
	 {
	 "code": "owner_staff",
	 "name": "自有员工",
	 "type": "ASSETS_TYPE_X"
	 },
	 {
	 "code": "goods",
	 "name": "货品",
	 "type": "ASSETS_TYPE_X"
	 },
	 {
	 "code": "offline_retail",
	 "name": "实体零售店",
	 "type": "ASSETS_TYPE_X"
	 },
	 {
	 "code": "material_factory",
	 "name": "原料/零部件工厂",
	 "type": "ASSETS_TYPE_X"
	 },
	 {
	 "code": "transport_suppliers",
	 "name": "运输供应商",
	 "type": "ASSETS_TYPE_X"
	 },
	 {
	 "code": "stock",
	 "name": "仓库",
	 "type": "ASSETS_TYPE_X"
	 },
	 {
	 "code": "labour_suppliers",
	 "name": "劳务供应商",
	 "type": "ASSETS_TYPE_X"
	 },
	 {
	 "code": "finished_factory",
	 "name": "成品工厂",
	 "type": "ASSETS_TYPE_X"
	 },
	 {
	 "code": "storage_equip",
	 "name": "仓储设备",
	 "type": "ASSETS_TYPE_X"
	 },
	 {
	 "code": "online_wholesale",
	 "name": "网上批发店",
	 "type": "ASSETS_TYPE_X"
	 },
	 {
	 "code": "offline_wholesale",
	 "name": "实体批发店",
	 "type": "ASSETS_TYPE_X"
	 }
	 ]
	 * @ApiMethodEnd
	 */
	@ResponseBody
	@RequestMapping("/getRelationCodes")
	public List<VCustomerServiceCode> getRelationCodes(@RequestBody() Map<String, Object> params) throws Exception {
		Object typeObecjt = params.get("type");
		if (null == typeObecjt) {
			return null;
		}
		String type = typeObecjt.toString();
		Object obj = params.get("code");
		if (null == obj) {
			return null;
		}
		String selectCode = obj.toString();
		List<CmCustomerServiceCode> selectCodes = getSelectCodes(selectCode, type);
		List<VCustomerServiceCode> vCustomerServiceCodes = convertCustomerServiceCode(selectCodes);
		VCustomerServiceCode vCustomerServiceCode = new VCustomerServiceCode();
		vCustomerServiceCode.setName("任意");
		vCustomerServiceCode.setCode("BASE");
		vCustomerServiceCodes.add(vCustomerServiceCode);
		return vCustomerServiceCodes;
	}

	//通过我是谁的选项和对应的type获取相关联的type下对应的code
	private List<CmCustomerServiceCode> getSelectCodes(String selectCode, String codeType) throws Exception {
		String serviceCodeType = "INDUSTRY_TYPE";
		List<CmCustomerServiceCode> serviceCodeList = cmBusinessServiceRuleMapper
				.getCodesByServiceCode(serviceCodeType, selectCode, codeType);
		return serviceCodeList;
	}

	// 取得客户类型
	private List<VCustomerServiceCode> convertCustomerServiceCode(List<CmCustomerServiceCode> cbsr) {
		List<VCustomerServiceCode> vCustomerServiceCodeList = new ArrayList<VCustomerServiceCode>();
		for (CmCustomerServiceCode business : cbsr) {
			VCustomerServiceCode vCustomerServiceCode = new VCustomerServiceCode();
			String type = business.getDefinedCodeType();
			String code = business.getDefinedCode();
			vCustomerServiceCode.setCode(code);
			vCustomerServiceCode.setType(type);
			String name = baDefinedCodeMapper.getdefinedname(type, code);//根据相应的type和code获取name
			vCustomerServiceCode.setName(name);
			vCustomerServiceCodeList.add(vCustomerServiceCode);
		}
		return vCustomerServiceCodeList;
	}


	//取得我是谁code及中文
	public List<BaIndustry> getByIndustries(List<BaIndustryExhibition> baIndustryExhibitions) {
		List<BaIndustry> baIndustries = new ArrayList<BaIndustry>();
		for (BaIndustryExhibition baIndustryExhibition : baIndustryExhibitions) {
			BaIndustry baIndustry = baIndustryMapper.get(baIndustryExhibition.getIndustryUkid());
			baIndustries.add(baIndustry);
		}
		return baIndustries;
	}

	// 取得我是谁
	private List<VCustomerServiceCode> convertServiceCodes(List<BaIndustry> cbsr) {
		List<VCustomerServiceCode> vCustomerServiceCodeList = new ArrayList<VCustomerServiceCode>();
		String type = "INDUSTRY_TYPE";
		for (BaIndustry baIndustry : cbsr) {
			VCustomerServiceCode vCustomerServiceCode = new VCustomerServiceCode();
			vCustomerServiceCode.setType(type);
			String code = baIndustry.getIndustryCode();
			vCustomerServiceCode.setCode(code);
			vCustomerServiceCode.setName(baIndustry.getIndustryName());
			vCustomerServiceCodeList.add(vCustomerServiceCode);
		}
		return vCustomerServiceCodeList;
	}

	private JSONArray getChildFunction(List<AuFunction> dataSource, Long auFunctionUkid) {
		JSONArray functionTree = new JSONArray();
		for (AuFunction auFunction : dataSource) {
			if (auFunctionUkid.equals(auFunction.getParentUkid())) {
				JSONObject json = new JSONObject();
				json.put("id", auFunction.getAuFunctionUkid());
				json.put("name", auFunction.getName());
				json.put("childFunction", getChildFunction(dataSource, auFunction.getAuFunctionUkid()));
				functionTree.add(json);
			}
		}
		return functionTree;
	}

	private Long getUkid() {
		int[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
		Random rand = new Random();
		for (int i = 10; i > 1; i--) {
			int index = rand.nextInt(i);
			int tmp = array[index];
			array[index] = array[i - 1];
			array[i - 1] = tmp;
		}
		int result = 0;
		for (int i = 0; i < 5; i++)
			result = result * 10 + array[i];
		result += 100000;
		return Long.valueOf(result);
	}

	private Long getTrueCardUkid() {
		Long ukid = getUkid();
		CfCardLabel cfCardLabel = cfCardLabelMapper.get(ukid);
		if (null != cfCardLabel) {
			ukid = getTrueCardUkid();
		}
		return ukid;
	}

	private Long getTrueUkid() {
		Long ukid = getUkid();
		CfOperationUnit cfOperationUnit = cfOperationUnitMapper.get(ukid);
		if (null != cfOperationUnit) {
			ukid = getTrueUkid();
		}
		return ukid;
	}
}
